import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'training_result_plot_page.dart';

class TrainingResultPage extends StatefulWidget {
  const TrainingResultPage({Key? key}) : super(key: key);

  @override
  State<TrainingResultPage> createState() => _TrainingResultPageState();
}

class _TrainingResultPageState extends State<TrainingResultPage> {
  late VideoPlayerController _videoPlayerController;
  bool _isVideoInitialized = false;

  // 训练结果文本
  final List<String> _resultLines = [
    "数据集：乳腺癌相关",
    "本地客户端数量：5",
    "本地模型：Cox模型",
    "联邦方法：Fedproc",
    "聚合方法：AggNet",
    "训练round：5",
    "本地训练轮数：100",
  ];

  @override
  void initState() {
    super.initState();
    // 设置状态栏颜色
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Color(0xFF1DC4A0),
      statusBarIconBrightness: Brightness.light,
    ));
    _initializeVideo();
  }

  Future<void> _initializeVideo() async {
    _videoPlayerController =
        VideoPlayerController.asset('assets/videos/training_video.mp4');
    try {
      await _videoPlayerController.initialize();
      // 开始播放视频
      await _videoPlayerController.play();
      if (mounted) {
        setState(() {
          _isVideoInitialized = true;
        });
      }
    } catch (e) {
      print('视频初始化失败: $e');
    }
  }

  @override
  void dispose() {
    _videoPlayerController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 顶部区域
          Container(
            width: double.infinity,
            padding: EdgeInsets.only(
              top: MediaQuery.of(context).padding.top,
              left: 20,
              right: 20,
              bottom: 15,
            ),
            decoration: const BoxDecoration(
              color: Color(0xFF1DC4A0),
            ),
            child: Row(
              children: [
                IconButton(
                  icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
                  onPressed: () => Navigator.pop(context),
                  padding: EdgeInsets.zero,
                ),
                const Text(
                  '训练结果',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
          ),

          // 主体内容
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // 视频播放区域
                  Container(
                    width: double.infinity,
                    height: MediaQuery.of(context).size.width,
                    color: Colors.black,
                    child: _isVideoInitialized
                        ? VideoPlayer(_videoPlayerController)
                        : const Center(
                            child: CircularProgressIndicator(
                              valueColor: AlwaysStoppedAnimation<Color>(
                                  Color(0xFF1DC4A0)),
                            ),
                          ),
                  ),

                  // 训练结果显示区域
                  Padding(
                    padding: const EdgeInsets.all(20.0),
                    child: Container(
                      width: double.infinity,
                      padding: const EdgeInsets.all(20),
                      decoration: BoxDecoration(
                        color: const Color(0xFF1DC4A0).withOpacity(0.1),
                        borderRadius: BorderRadius.circular(15),
                        border: Border.all(
                          color: const Color(0xFF1DC4A0).withOpacity(0.3),
                          width: 1,
                        ),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          const Text(
                            "训练结果",
                            style: TextStyle(
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                              color: Color(0xFF1DC4A0),
                            ),
                          ),
                          const SizedBox(height: 15),
                          ..._resultLines.map((line) => Padding(
                                padding:
                                    const EdgeInsets.symmetric(vertical: 4),
                                child: Text(
                                  line,
                                  style: const TextStyle(
                                    fontSize: 16,
                                    fontWeight: FontWeight.w500,
                                  ),
                                  textAlign: TextAlign.center,
                                ),
                              )),
                          const SizedBox(height: 20),
                          ElevatedButton(
                            style: ElevatedButton.styleFrom(
                              backgroundColor: const Color(0xFF1DC4A0),
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(10)),
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 30, vertical: 12),
                            ),
                            onPressed: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (_) =>
                                        const TrainingResultPlotPage()),
                              );
                            },
                            child: const Text(
                              '训练结果可视化',
                              style:
                                  TextStyle(color: Colors.white, fontSize: 16),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
